﻿using System.Runtime.CompilerServices;

namespace Common.Class
{
    public class LogHelper
    {

        /// <summary>
        /// 日志文件存放路径
        /// </summary>
        private static string logPath = string.Empty;

        ///<summary> 
        /// 保存日志的文件夹 
        ///</summary> 
        private static string LogPath()
        {
            if (logPath == string.Empty)
            {
                logPath = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) + @"\log\";
            }
            return logPath;
        }

        /// <summary>
        /// 同步写日志
        /// </summary>
        /// <param name="logType">日志文件类型</param>
        /// <param name="msg">日志内容</param>
        /// <param name="fileName">日志文件</param>
        [MethodImpl(MethodImplOptions.Synchronized)]
        public static void WriteLogToFile(string logType, string msg, string fileName = "Default")
        {

            string _filePath = LogPath() + logType + @"\";
            if (!Directory.Exists(_filePath))
            {
                Directory.CreateDirectory(_filePath);
            }
            System.IO.File.AppendAllText(_filePath + fileName + DateTime.Now.ToString("yyyyMMdd") + ".Log",
                DateTime.Now.ToString("HH:mm:ss.fff") + "      " + msg + "\r\n");

        }

    }
}
